/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Model.Model_User_Account;
import Util.DataUtil;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author TuanDat
 */
public class DAO_User_Account {

    public Model_User_Account get_UserAccountByID_RolesID(int userID, int roleID) {
        Model_User_Account user = new Model_User_Account();

        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{call sp_GetUserAccountByID_RoleID(?,?)}");
            csmt.setInt("ID", userID);
            csmt.setInt("Roles_ID", roleID);
            ResultSet rs = csmt.executeQuery();
            while (rs.next()) {
                user.setId(rs.getString("ID"));
                user.setRole_id(rs.getString("Role_ID"));
                user.setUsername(rs.getString("Username"));
                user.setPassword(rs.getString("Password"));
                user.setName(rs.getString("Name"));
                user.setPhone_no(rs.getString("Phone_No"));
                user.setEmail(rs.getString("Email"));
                user.setAmmount(rs.getString("Ammount"));
                user.setLast_played(rs.getString("Last_Played"));
                user.setLast_update(rs.getString("Last_Update"));


            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return user;
    }

    public ArrayList<Model_User_Account> getAllUser_Account() {
        ArrayList<Model_User_Account> listUser = new ArrayList<Model_User_Account>();

        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{call sp_GetAllUser_Account}");
            ResultSet rs = csmt.executeQuery();
            while (rs.next()) {
                Model_User_Account user = new Model_User_Account();
                user.setId(rs.getString("ID"));
                user.setRole_id(rs.getString("Role_ID"));
                user.setUsername(rs.getString("Username"));
                user.setPassword(rs.getString("Password"));
                user.setName(rs.getString("Name"));
                user.setPhone_no(rs.getString("Phone_No"));
                user.setEmail(rs.getString("Email"));
                user.setAmmount(rs.getString("Ammount"));
                user.setLast_played(rs.getString("Last_Played"));
                user.setLast_update(rs.getString("Last_Update"));

                listUser.add(user);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return listUser;
    }

    public boolean insertAnswer(Model_User_Account user) {
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{call sp_insertUser_Account(?,?,?,?,?,?,?,?)}");
            csmt.setString("Role_ID", user.getRole_id());
            csmt.setString("Username", user.getUsername());
            csmt.setString("Password", user.getPassword());
            csmt.setString("Name", user.getName());
            csmt.setString("Phone_No", user.getPhone_no());
            csmt.setString("Email", user.getEmail());
            csmt.setString("Ammount", user.getAmmount());
            csmt.setString("Last_Played", user.getLast_played());

            csmt.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Question.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }

    public int UpdateAccount(Model_User_Account user) {
        int result = 0;
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{call sp_insertUser_Account(?,?,?,?,?,?,?,?,?)}");
            csmt.setString("ID", user.getId());
            csmt.setString("Role_ID", user.getRole_id());
            csmt.setString("Username", user.getUsername());
           // csmt.setString("Password", user.getPassword());
            csmt.setString("Name", user.getName());
            csmt.setString("Phone_No", user.getPhone_no());
            csmt.setString("Email", user.getEmail());
            csmt.setString("Ammount", user.getAmmount());
            csmt.setString("Last_Played", user.getLast_played());
            csmt.registerOutParameter("RESULT", java.sql.Types.INTEGER);

            csmt.execute();
            result = csmt.getInt("RESULT");
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Question.class.getName()).log(Level.SEVERE, null, ex);
            return result = 0;
        }
        return result;
    }
}
